package com.kylin.module.community.actions;

import com.kylin.common.paginate.Paging;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import java.util.List;
import java.util.Locale;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.kylin.common.Constants;

import com.kylin.module.community.business.FriendsBlogic;
import com.kylin.module.community.orm.Friendslist;

import com.kylin.common.exception.BusinessException;

import com.kylin.hibernate.BaseHibernateDAO;
import com.kylin.module.user.orm.User;

import com.kylin.module.community.business.MemberBlogic;
import com.kylin.module.community.forms.MemberForm;
import com.kylin.struts.BaseActionInterface;
import com.kylin.struts.BaseDispatchAction;

import com.kylin.common.util.MemObjFactory;


public class FriendsAction  extends BaseDispatchAction implements BaseActionInterface {

	protected static Logger log = Logger.getLogger(FriendsAction.class);	
	
	public ActionForward unspecified(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("in friends.unspecified");
		MemberForm form = (MemberForm)actionForm;
		String forward = "show";
		try{
			
			Locale locale = getSessionLocale(request);//new Locale("zh","CN");
			
			//form.setLanguage(getSessionLocale(request).toString());
			//MemberBlogic bean = new MemberBlogic();
			//bean.addMember(form);
			log.info("保存用户信息："+ form.getUsername());	
		    List countryList = MemObjFactory.getAllCountryList(locale);
		    request.setAttribute("countryList",countryList);
		}catch (Exception e) {
			forward = doException( request, e, forward);
		}
		return mapping.findForward(forward);
	}

	public ActionForward save(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("开始保存用户信息 : "+request.getParameter("name"));
		MemberForm form = (MemberForm)actionForm;
		String forward = "addSucc";
		try{
			form.setLanguage(getSessionLocale(request).toString());
			MemberBlogic bean = new MemberBlogic();
			bean.addMember(form);
			log.info("保存用户信息："+ form.getUsername());
		}catch (Exception e) {
			forward = doException( request, e, "add");
		}
		return mapping.findForward(forward);
	}

	public ActionForward search(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}	

	public ActionForward add(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("开始保存用户信息 : "+request.getParameter("name"));
		String forward = "success";
		log.info("=====================add=======================");
		String user2id = null;
		
		User user = (User)request.getSession().getAttribute(Constants.CURR_USER);
		//Long userid = new Long("1");
		//User user = BaseHibernateDAO.
		
		/* not login or vendor need register and then add friend 
		 * 
		 * */
		if (user == null || user.getUserrole().intValue() == 1 ){
			
			forward = "login";
			request.setAttribute("forward", "friend.add");
			request.setAttribute("uid", request.getParameter("uid"));
			
			return mapping.findForward(forward);
			
		}else{		
		
			user2id = request.getParameter("uid");
		
			try{
				FriendsBlogic bean = new FriendsBlogic();
				
				bean.addFriend(user, new Long(user2id));
				
				
				log.info("保存friends list：");
			}catch (Exception e) {
				forward = doException( request, e, forward);
			}
		
		
		
			Locale locale = getSessionLocale(request);//(Locale) session.getAttribute(Action.LOCALE_KEY);
			ActionMessages messages = new ActionMessages();
			messages.add("msg", new ActionMessage("好友添加成功"));
			saveMessages(request, messages);		
			
			String path = mapping.findForward("add").getPath();
			if (user2id != null){
				path = "/module/community/member/post.do?task=mypost&msg=ok&uid="+user2id;
			}
			else{
				path ="/enter/loginEnter.jsp";
			}
			log.info("path: "+path);
	
			ActionForward actionforward = new ActionForward(path);
			actionforward.setRedirect(true);
			return actionforward;
		}

	
	}		
	
	public ActionForward delete(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String path = "";//mapping.findForward("add").getPath();
		path = "/module/community/member/post.do?task=mypost";		
		try{
			Long id = new Long(request.getParameter("id"));
			//log.info("friendslist delete " + id.toString());
			BaseHibernateDAO.deleteById(Friendslist.class, id);
			
		}
		catch(BusinessException be){
			log.info("exception:"+be.toString());
		}
		//log.info("path: "+path);
		//log.info("Refer "+ request.get);
		ActionForward actionforward = new ActionForward(path);
		actionforward.setRedirect(true);
		return actionforward;
	}

	public ActionForward lastFriends(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.info("last friends : searchId="+request.getParameter("searchId"));
		String forward = "lastReg";
		try {

			MemberBlogic bean = new MemberBlogic();
			List list = bean.getLastReg();
			//list.get(index)
			request.setAttribute(Constants.KEY_REQUEST_RESULT, list);	
			
		}catch (Exception e) {
			doException( request, e, "search");
		}
		return mapping.findForward(forward);
	}

	public ActionForward show(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		
		String forward = "show";
		
		Long uid = null;
		if(request.getParameter("uid") != null){
			uid = new Long (request.getParameter("uid"));
		}
		Integer limitstart = new Integer("0");
		Integer limit = new Integer("10");
		
		if(1>2){
			if(request.getParameter("limitstart") != null )	{		
				limitstart = new Integer (request.getParameter("limitstart"));
			}	
			if(request.getParameter("limit") != null ){
				limit = new Integer (request.getParameter("limit"));
			}		
		}
		try {

			FriendsBlogic bean = new FriendsBlogic();
			
			//User user = (User)request.getSession().getAttribute(Constants.CURR_USER);
			//if Integer.parseInt(sid)
			Long userid = null;
			User user = null;
			if (uid == null || uid.intValue() == 0){
				  if(request.getSession().getAttribute(Constants.CURR_USER)!= null){
					  user = (User)request.getSession().getAttribute(Constants.CURR_USER);
					  uid = user.getId();
					  
				  }
				  else{
					  return mapping.findForward("login");
				  }
			}			
			else{
				user =  (User)BaseHibernateDAO.findById(User.class, uid);
			}
			
			Paging page = bean.show(user, limitstart, limit);
			//List list = bean.list(userid);
			//list.get(index)
			//if (list != null){
			log.info("uid :"+ uid);
			//request.setAttribute("friendslist", list);
			request.setAttribute("uid", uid);
			request.setAttribute("friendslists", page);
			//}
			
			//User user = BaseHibernateDAO.
			//if (user == null){
			//	forward = "login";
			//}else{
				
				//log.info(" userid :"+ user.getId());

			//}
			
		}catch (Exception e) {
			doException( request, e, forward);
		}
		log.info("in friends.show : " + forward);
		return mapping.findForward(forward);
	}
		
	public ActionForward edit(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response){

		return null;
	}

	public ActionForward update(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String forward = "list.do";
		log.info("forward :"+forward );
		log.info("forward id:"+((MemberForm)actionForm).getId());
		//log.info("查询属性表 :"+ ((MemberForm)actionForm).toString() );
		MemberBlogic bean = new MemberBlogic();	
		try{
			bean.updateMember((MemberForm)actionForm);
		}catch (Exception e) {
			log.info("forward exception:"+e.toString() );
			forward = doException( request, e, "update");
		}
		
		return mapping.findForward(forward);
	}

}
